package com.sc.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sc.nft.dao.MallDrawActivityPrizePoolDao;
import com.sc.nft.dao.MallDrawActivityPrizePoolDetailDao;
import com.sc.nft.entity.MallDrawActivityPrizePool;
import com.sc.nft.entity.MallDrawActivityPrizePoolDetail;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityPrizePoolDTO;
import com.sc.nft.mapstruct.MallDrawActivityMapStruct;
import com.sc.nft.service.MallDrawActivityPrizePoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallDrawActivityPrizePoolServiceImpl implements MallDrawActivityPrizePoolService {

    @Autowired
    private MallDrawActivityPrizePoolDao mallDrawActivityPrizePoolDao;

    @Autowired
    private MallDrawActivityPrizePoolDetailDao mallDrawActivityPrizePoolDetailDao;

    @Override
    public List<MallDrawActivityPrizePool> listByActivityId(Long activityId) {
        List<MallDrawActivityPrizePool> result = mallDrawActivityPrizePoolDao.getByActivityId(activityId);
        int index = 1;
        for (MallDrawActivityPrizePool prizePool : result) {
            prizePool.setPoolIndex(index);
            index++;
        }
        return result;
    }

    @Override
    public List<MallDrawActivityPrizePoolDTO> listByActivityIdToDTO(Long activityId) {
        List<MallDrawActivityPrizePoolDTO> mallDrawActivityPrizePoolDTOList = new ArrayList<>();
        List<MallDrawActivityPrizePool> result = mallDrawActivityPrizePoolDao.getByActivityId(activityId);
        for (MallDrawActivityPrizePool mallDrawActivityPrizePool : result) {
            MallDrawActivityPrizePoolDTO mallDrawActivityPrizePoolDTO = MallDrawActivityMapStruct.INSTANCE.mallDrawActivityPrizePool2DTO(mallDrawActivityPrizePool);
            LambdaQueryWrapper<MallDrawActivityPrizePoolDetail> drawActivityPrizePoolDetailWrapper = new LambdaQueryWrapper<>();
            drawActivityPrizePoolDetailWrapper.eq(MallDrawActivityPrizePoolDetail::getPoolId, mallDrawActivityPrizePool.getId());
            List<MallDrawActivityPrizePoolDetail> mallDrawActivityPrizePoolDetailList = mallDrawActivityPrizePoolDetailDao.list(drawActivityPrizePoolDetailWrapper);
            for (MallDrawActivityPrizePoolDetail mallDrawActivityPrizePoolDetail : mallDrawActivityPrizePoolDetailList) {
                if (mallDrawActivityPrizePoolDetail.getPrizeCount() > 0) {
                    mallDrawActivityPrizePoolDetail.setRemainingPrizeCount(mallDrawActivityPrizePoolDetail.getRemainingPrizeCount() / mallDrawActivityPrizePoolDetail.getPrizeCount());
                    mallDrawActivityPrizePoolDetail.setTotalPrizeCount(mallDrawActivityPrizePoolDetail.getTotalPrizeCount() / mallDrawActivityPrizePoolDetail.getPrizeCount());
                }
            }
            mallDrawActivityPrizePoolDTO.setMallDrawActivityPrizePoolDetailList(MallDrawActivityMapStruct.INSTANCE.mallDrawActivityPrizePoolDetail2DTO(mallDrawActivityPrizePoolDetailList));
            mallDrawActivityPrizePoolDTOList.add(mallDrawActivityPrizePoolDTO);
        }
        return mallDrawActivityPrizePoolDTOList;
    }

    @Override
    public MallDrawActivityPrizePool getById(Long id) {
        return mallDrawActivityPrizePoolDao.getById(id);
    }


    @Override
    public Integer countByActivityId(Long activityId) {
        return mallDrawActivityPrizePoolDao.countByActivityId(activityId);
    }

    @Override
    public Boolean update(MallDrawActivityPrizePool mallDrawActivityPrizePool) {
        return null;
    }
}
